﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;
namespace DAO
{
    public class KhachHangDAO : Abstract
    {
        public static bool ThemKhachHang(KhachHangDTO _khachHang)
        {
            string query = "insert into KhachHang (HoTen, NgaySinh, CMNDPassport,LaKhachTrongNuoc) values ('{0}','{1}',{2},{3})"
                .Replace("{0}", _khachHang.HoTen)
                .Replace("{1}", _khachHang.NgaySinh.ToShortDateString())
                .Replace("{2}", _khachHang.Cmnd_Passport)
                .Replace("{3}", _khachHang.LaKhachTrongNuoc == true?"1":"0");
            return Query(query);
        }
        public static bool SuaThongTinKhachHang(KhachHangDTO _khachHang)
        {
            string query = "update KhachHang set HoTen = '{0}, NgaySinh = '{1}', CMNDPassport = '{2}',LaKhachTrongNuoc = '{3}' where MaKhachHang = {4}"
               .Replace("{0}", _khachHang.HoTen)
               .Replace("{1}", _khachHang.NgaySinh.ToShortDateString())
               .Replace("{2}", _khachHang.Cmnd_Passport)
               .Replace("{3}", _khachHang.LaKhachTrongNuoc.ToString())
               .Replace("{4}",_khachHang.MaKhachHang.ToString());
            return Query(query);
        }
  
        public static List<KhachHangDTO> LayDSKhachHang(int maKhachHang = -1, string hoTen = null, string cmnd_Passport = null)
        {
            OleDbConnection conn = null;
            List<KhachHangDTO> listKhachHang = new List<KhachHangDTO>();
            try
            {
                conn = GetConnection();
                conn.Open();
            }
            catch
            {
                throw new Exception("Connect error!");
            }
            try
            {
                string query = "select MaKhachHang, HoTen, NgaySinh, CMNDPassport,LaKhachTrongNuoc from KhachHang ";
                if (maKhachHang != -1)
                    query = query + "where MaKhachHang = {0}".Replace("{0}", maKhachHang.ToString());
                if (hoTen != null)
                    query = query + "where HoTen = '{1}'".Replace("{1}", hoTen);
                else if( cmnd_Passport != null)
                    query = query + "where CMNDPassport = '{2}'".Replace("{2}", cmnd_Passport);
                OleDbDataReader rd = new OleDbCommand(query, conn).ExecuteReader();
                while (rd.Read())
                {
                    KhachHangDTO khachHang = new KhachHangDTO();
                    khachHang.MaKhachHang = rd.GetInt32(0);
                    khachHang.HoTen = rd.GetString(1);
                    khachHang.NgaySinh = rd.GetDateTime(2);
                    khachHang.Cmnd_Passport = rd.GetString(3);
                    khachHang.LaKhachTrongNuoc = rd.GetBoolean(4);
                    listKhachHang.Add(khachHang);
                }
            }
            catch (Exception e)
            {
                listKhachHang = null;
            }

            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
            return listKhachHang;
        }
    }
}
